package com.ojoin.trade.supplyloan_core.utils;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import com.ojoin.trade.common.Constants;

public class HttpClientTest {

	public static void main(String[] args) throws Exception {
		String url = "http://vcp.jd.com/sub_procurement/procurement/orderList";
		String host = "vcp.jd.com";
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost(host, 80, "http");

		HttpMethod method = postMethod(url);
		httpClient.executeMethod(method);

//		String response = method.getResponseBodyAsString();
//		BufferedReader reader = new BufferedReader(new InputStreamReader(
//				method.getResponseBodyAsStream()));
		System.out.println(method.getResponseBody().length);
		System.out.println(new String(method.getResponseBody(), Constants.ENCODING));
//		while(true) {
//			String s = reader.readLine();
//			if (s == null ) {
//				break;
//			}
//			System.out.println(s);
//		}
		// String response = new
		// String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));
//		System.out.println(response);
	}

	private static HttpMethod getMethod(String url, String param)
			throws IOException {
		GetMethod get = new GetMethod(url + "?" + param);
		get.releaseConnection();
		return get;
	}

	private static HttpMethod postMethod(String url) throws IOException {
		PostMethod post = new PostMethod(url);
		post.setRequestHeader("Accept", "application/json, text/javascript, */*");
		post.setRequestHeader("Accept-Encoding", "gzip, deflate");
		post.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
		post.setRequestHeader("Cache-Control", "no-cache");
		post.setRequestHeader("Connection", "keep-alive");
		post.setRequestHeader("Content-Length", "71");
		post.setRequestHeader("Cookie", "lighting=64704ABB4FEFAA7DE2A835AA53417B975E5F34A25881ABB17260F82DB732BA9EA35D47B412A6265E0B7EFA83BBF2B9C4B7EEFE03655E952E2995DCDF57182850F48028FF6A8B2394E68C3FA6FAC91AEA18268528F216A5780E746B28E32D05F25A665284890254FA7A831D6C12B48EB25715D4F08FAE10602DC08D50D32D9B2F6E0181B2D8D9DD4BE9D7E8841A38D791; user-key=3d404412-392b-4980-956d-337b1236a368; jwotest_product=98; mba_muid=1438699242760-365b910d29251d84a0; mt_xid=V2_52007VwMSVFRRUl4YTh9sVWELEQcIXAdGTEsbXRliBBQHQQtXWhdVHFsGZ1FHUQ5YVVsfeRpdBWMfElNBWVpLH0oSXQJsABViX2hRahtKH1wAYDMSVlw%3D; unpl=V2_ZzNtbRdTFBFzWxVSKxoOA2JWFVwSUEUddQ5PVysbWFFjV0FdclRCFXEURlVnGFQUZwEZXUpcQx1FCHZWfBBYA1czZ1xDVBdLKkxDDD4cXF4wC0sWESRzFXw4Q2R7GVoMbgMWWkVRcyVyOHZkeildNWY%3d; mt_subsite=||80%2C1444367959; cn=73; ipLocation=%u5317%u4EAC; areaId=1; ipLoc-djd=1-2901-4135-0; VC_INTEGRATION_JSESSIONID=ece149ca-17d0-4211-982d-37fa72249eb7; __jda=122270672.1178815667.1406816828.1444371360.1444381309.253; __jdb=122270672.1.1178815667|253.1444381309; __jdc=122270672; __jdv=122270672|fun.fanli.com|t_36857_|tuiguang|d5f57bb7a2c74d60a6791782a35e5eb1; __jdu=1178815667; _tp=MMAgxw75JQRWU%2FZJi67rRZNIi6zk1jOXIQyBLD3LtYk%3D; _pst=jd_4d3791da589fb; TrackID=16k-8RopJDW_wx-dgKyiDfv5ZFEi7qOlnGaECLHY848RPLWcQzPUdW9RQ1v0uIjvAW9XmW7Q283V3VwoxnXoCIvGrsAmK2CB0TTvMjswlz3o; pinId=0P1EO47V_CYe8GByxmhkXrV9-x-f3wj7; logining=1; unick=jd_huanme518; pin=jd_4d3791da589fb; thor=61837CE33D0FC9A2842214A6A13DB784108B19C88B342DE7B89B4098183B4D23C50DCE0C8B017CBE9E0B818158D475BDF5C8C37897124FC689432F123CDF09C51D56157D538F15DA6A0A6F7D735793A234331BE11B7963DDFFA755162413E5F555AD00034887FA43070FF44857F05EA6F1E7E96F1241C915D02FBD186AF5CA9F40DE76695F1782E169F6A229DCC5727CC080C5EEF2CC5DF486C6A3000BCE1BBA");
		post.setRequestHeader("Host", "vcp.jd.com");
		post.setRequestHeader("Origin", "http://vcp.jd.com");
		post.setRequestHeader("Pragma", "no-cache");
		post.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36");
		post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		post.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		post.setRequestHeader("Referer", "http://vcp.jd.com/sub_procurement/procurement/initListPage");
		
		NameValuePair[] param = { 
				new NameValuePair("length", "100"),
				new NameValuePair("page", "1"),
				new NameValuePair("sidx", ""),
				new NameValuePair("sord", ""),
				new NameValuePair("stateList", "-1"),
				new NameValuePair("confirmStateList", "-1"),
				new NameValuePair("status", "1") };
		
		post.setRequestBody(param);
		post.releaseConnection();
		return post;
	}
}